package com.leo.ibatis.demo.dao.ibatis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.util.Assert;

import com.leo.ibatis.demo.criteria.OrderstatusCriteria;
import com.leo.ibatis.demo.dao.OrderstatusDao;
import com.leo.ibatis.demo.domain.Orderstatus;
import com.leo.ibatis.demo.util.Paging;

public class OrderstatusDaoImpl extends SqlMapClientDaoSupport implements OrderstatusDao {

	public void insert(Orderstatus orderstatus) throws DataAccessException {
		getSqlMapClientTemplate().insert("Orderstatus.insert", orderstatus);
	}

	public int update(Orderstatus orderstatus) throws DataAccessException {
		int actualRowsAffected = 1;

		getSqlMapClientTemplate().update("Orderstatus.update", orderstatus, actualRowsAffected);

		return actualRowsAffected;
	}

	public int delete(int linenum, int orderid) throws DataAccessException {
		int actualRowsAffected = 1;

		Map<String, Object> param = new HashMap<String, Object>(2);
		param.put("linenum", new Integer(linenum));
		param.put("orderid", new Integer(orderid));

		getSqlMapClientTemplate().delete("Orderstatus.delete", param, actualRowsAffected);

		return actualRowsAffected;
	}

	public Orderstatus select(int linenum, int orderid) throws DataAccessException {
		Map<String, Object> param = new HashMap<String, Object>(2);
		param.put("linenum", new Integer(linenum));
		param.put("orderid", new Integer(orderid));

		return (Orderstatus) getSqlMapClientTemplate().queryForObject("Orderstatus.select", param);
	}

	@SuppressWarnings("unchecked")
	public List<Orderstatus> list(OrderstatusCriteria orderstatusCriteria) throws DataAccessException {
		Assert.notNull(orderstatusCriteria, "orderstatusCriteria must not be null");
		
		return getSqlMapClientTemplate().queryForList("Orderstatus.list", orderstatusCriteria);
	}

	@SuppressWarnings("unchecked")
	public List<Orderstatus> listOnPage(OrderstatusCriteria orderstatusCriteria) throws DataAccessException {
		Assert.notNull(orderstatusCriteria, "orderstatusCriteria must not be null");
		Assert.notNull(orderstatusCriteria.getPaging(), "paging must not be null");
		
		int totalResults = count(orderstatusCriteria);
		Paging paging = orderstatusCriteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return getSqlMapClientTemplate().queryForList("Orderstatus.list", orderstatusCriteria, paging.getFirstResult(), paging.getMaxResults());
	}

	public int count(OrderstatusCriteria orderstatusCriteria) throws DataAccessException {
		Assert.notNull(orderstatusCriteria, "orderstatusCriteria must not be null");
		
		return ((Integer) getSqlMapClientTemplate().queryForObject("Orderstatus.count", orderstatusCriteria)).intValue();
	}

}
